package com.dianjing.dianjing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dianjing.dianjing.controller.UploadController;
import com.dianjing.dianjing.dto.input.PaintingPageInput;
import com.dianjing.dianjing.dto.input.WorksInput;
import com.dianjing.dianjing.dto.output.PaintingOutput;
import com.dianjing.dianjing.dto.output.WorksDetailOutput;
import com.dianjing.dianjing.entity.Painting;
import com.dianjing.dianjing.entity.User;
import com.dianjing.dianjing.mapper.PaintingMapper;
import com.dianjing.dianjing.service.PaintingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dianjing.dianjing.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author owf
 * @since 2022-11-11
 */
@Service
public class PaintingServiceImpl extends ServiceImpl<PaintingMapper, Painting> implements PaintingService {

    @Resource
    private PaintingMapper paintingMapper;

    @Resource
    private UserService userService;

    @Resource
    private UploadController uploadController;


    /**
     * 作品展示列表
     *
     * @param input
     * @return
     */
    @Override
    public IPage<Painting> show(PaintingPageInput input) {
        Page<Painting> paintingPage = new Page<>(input.getPageNumber(), input.getPageSize());
        LambdaQueryWrapper<Painting> queryWrapper = new LambdaQueryWrapper<>();
        if (input.getType() != 0) {
            queryWrapper.eq(Painting::getType, input.getType());
        }
        queryWrapper.orderByDesc(Painting::getCreateTime);
        return paintingMapper.selectPage(paintingPage, queryWrapper);
    }

    /**
     * 作品详情
     *
     * @param paintingId
     * @return
     */
    @Override
    public PaintingOutput detail(Long paintingId) {
        return paintingMapper.detail(paintingId);
    }

    @Override
    public Boolean worksUpload(WorksInput worksInput) {
        // 上传文件，得到系统可访问的url http://ip:port/{url}
        String url = uploadController.MultipartFileToUrl(worksInput.getWorks());
        // 如果上传失败，直接上传作品失败
        if(url == null) {
            return false;
        }
        // 保存作品信息
        Painting painting = new Painting();
        BeanUtils.copyProperties(worksInput, painting);
        painting.setPaintingUrl(url);
        this.save(painting);
        return true;
    }

    @Override
    public List<Painting> seeMyWorks(Long painterId) {
        // 直接从作品表中查画师的作品
        LambdaQueryWrapper<Painting> wrapper = new LambdaQueryWrapper<Painting>().eq(Painting::getPainterId, painterId);
        List<Painting> paintings = baseMapper.selectList(wrapper);
        return paintings;
    }

    @Override
    public List<Painting> seeWorks(Long typeId) {
        LambdaQueryWrapper<Painting> wrapper = new LambdaQueryWrapper<>();
        // 如果typeId为空查询全部
        wrapper.eq(typeId != null,Painting::getType,typeId);
        // 根据创建的时间进行排序
        wrapper.orderByDesc(Painting::getCreateTime);
        // 查询
        List<Painting> paintings = baseMapper.selectList(wrapper);
        return paintings;
    }

    @Override
    public WorksDetailOutput worksDetails(Long worksId) {
        WorksDetailOutput worksDetailOutput = new WorksDetailOutput();
        // 获取作品信息
        Painting painting = baseMapper.selectById(worksId);
        BeanUtils.copyProperties(painting,worksDetailOutput);
        // 根据作品中的画师id，查询画师信息
        User painter = userService.getById(painting.getPainterId());
        // 将画师的信息放入返回的对象中
        worksDetailOutput.setPainterNickname(painter.getNickname());
        worksDetailOutput.setPainterHeaderUrl(painter.getHeaderUrl());
        return worksDetailOutput;
    }

    @Override
    public List<Painting> seeWorks(Long painterId, Long typeId) {
        // 直接构造条件查询
        LambdaQueryWrapper<Painting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Painting::getPainterId,painterId)
                .eq(typeId != null, Painting::getType,typeId);
        List<Painting> paintings = baseMapper.selectList(wrapper) ;
        return paintings;
    }
}
